package rkw.database.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Type;

import core.communication.electionConfiguration.AddEditElectionDetailsElectionConfiguration;
import core.database.AbstractEntity;
import core.database.ElectionStatus;

@Entity
@Table(name = "election")
public class Election extends AbstractEntity {
	@Column(name = "identificationField", nullable = false)
	private Long identificationField;

	@Column(name = "name", nullable = false)
	private String name;

	@Column(name = "description")
	@Type(type = "text")
	private String description;

	@Column(name = "startDate", nullable = false)
	@Type(type = "timestamp")
	private Date startDate;

	@Column(name = "endDate", nullable = false)
	@Type(type = "timestamp")
	private Date endDate;

	@Column(name = "status", nullable = false)
	private ElectionStatus status;

	@Column(name = "cards")
	@Type(type = "text")
	private String cards;

	@OneToMany(mappedBy = "election", fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE }, orphanRemoval = true)
	private List<ElectionVoter> voters = new ArrayList<ElectionVoter>();

	@OneToMany(mappedBy = "election", fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE }, orphanRemoval = true)
	private List<AdministratorToken> administratorTokens = new ArrayList<AdministratorToken>();

	@OneToMany(mappedBy = "election", fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE }, orphanRemoval = true)
	private List<Vote> votes = new ArrayList<Vote>();

	public Election() {
		super();
		this.status = ElectionStatus.UNSTARTED;
	}

	public Long getIdentificationField() {
		return identificationField;
	}

	public void setIdentificationField(Long identificationField) {
		this.identificationField = identificationField;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public ElectionStatus getStatus() {
		return status;
	}

	public void setStatus(ElectionStatus status) {
		this.status = status;
	}

	public String getCards() {
		return cards;
	}

	public void setCards(String cards) {
		this.cards = cards;
	}

	public List<ElectionVoter> getVoters() {
		return voters;
	}

	public void setVoters(List<ElectionVoter> voters) {
		this.voters = voters;
	}

	public List<AdministratorToken> getAdministratorTokens() {
		return administratorTokens;
	}

	public void setAdministratorTokens(List<AdministratorToken> administratorTokens) {
		this.administratorTokens = administratorTokens;
	}

	public List<Vote> getVotes() {
		return votes;
	}

	public void setVotes(List<Vote> votes) {
		this.votes = votes;
	}

	public void setElectionDetails(AddEditElectionDetailsElectionConfiguration conf) {
		this.identificationField = conf.getElectionIdentificationField();
		this.name = conf.getName();
		this.description = conf.getDescription();
		this.startDate = conf.getStartDate();
		this.endDate = conf.getEndDate();
		this.cards = conf.getCards();
	}
}
